<?php
class Log_file extends Log
{
	public $id;
	protected $_opened=false;
	protected $_fh;
	protected $_filename='php.log';
	protected $_logdir='logs/';
	protected $_mode=0644;
	protected $_dirmode=0755;
	protected $_append=true;
	protected $_eol="\n";
	protected function __construct($name,$conf=array(),$level='')
	{
		$this->id=md5(microtime());
		if (!empty($name)) {
			$this->_filename=$name;
		}
		if (isset($conf['append'])) {
			$this->_append=$conf['append'];
		}
		if (isset($conf['eol'])) {
			$this->_eol=$conf['eol'];
		}
		if (isset($conf['mode'])) {
			$this->_mode=$conf['mode'];
		}
		if (isset($conf['dirmode'])) {
			$this->_dirmode=$conf['dirmode'];
		}
	}
	
	protected function _mkpath($path,$mode=0700)
	{
		$head=dirname($path);
		$tail=basename($path);
		if (empty($tail)) {
			$head=$path;
			$tail=$path;
		}
		if (!empty($head) && !empty($tail) && !is_dir($head)) {
			$this->_mkpath($head,$mode);
		}
		return @mkdir($head);
	}
	
	protected function open()
	{
		if (!$this->_opened) {
			/*if (!is_dir(dirname($this->_filename))) {
				$this->_mkpath($this->_filename,$this->_dirmode);
			}*/
			$creating=!file_exists($this->_logdir.$this->_filename);
			$this->_fh=@fopen($this->_logdir.$this->_filename,($this->_append)?'a':'w');
			$this->_opened=($this->_fh!==false);
			if ($creating && $this->_opened) {
				chmod($this->_logdir.$this->_filename,$this->_mode);
			}
		}
		return $this->_opened;
	}

	public function close()
	{
		if ($this->opened && fclose($this->_fh)) {
			$this->_opened=false;
		}
		return ($this->_opened===false);
	}

	public function log($message,$priority=null)
	{
		if (!$this->_oppened && !$this->open()) {
			return false;
		}
		$line=$message.$this->_eol;
		$success=(fwrite($this->_fh,$line)!==false);
		return $success;
	}
}
?>